﻿var _menus = { "menus": [{ "menuid": "1", "icon": "icon-sys", "menuname": "导航菜单", "menus": [
				{ "menuid": "12", "menuname": "产品管理", "icon": "icon-list", "url": "ProductsList.aspx" },
                { "menuid": "14", "menuname": "新闻管理", "icon": "icon-list", "url": "NewsList.aspx" },
                { "menuid": "15", "menuname": "用户管理", "icon": "icon-list", "url": "Users.aspx" },
                { "menuid": "16", "menuname": "字典管理", "icon": "icon-list", "url": "Dict.aspx" }
             ]
}]
};
$(function () {
    InitLeftMenu();

    $('#tabs').tabs({
        tools: [{
            iconCls: 'icon-exit',
            text: "安全退出",
            handler: function () {
                window.location.href = "Logout.aspx";
            }
        }]
    });
})

//初始化左侧
function InitLeftMenu() {
    $("#nav").accordion({ animate: false });
    $.each(_menus.menus, function (i, n) {
        var menulist = '';
        menulist += '<ul>';
        $.each(n.menus, function (j, o) {
            menulist += '<li><div><a ref="' + o.menuid + '" href="#" rel="' + o.url + '" ><span class="icon ' + o.icon + '" >&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div></li> ';
        })
        menulist += '</ul>';

        $('#nav').accordion('add', {
            title: n.menuname,
            content: menulist,
            iconCls: 'icon ' + n.icon
        });

    });

    $('.easyui-accordion li a').click(function () {
        var tabTitle = $(this).children('.nav').text();

        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        var icon = getIcon(menuid, icon);

        addTab(tabTitle, url, icon);
        $('.easyui-accordion li div').removeClass("selected");
        $(this).parent().addClass("selected");
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });

    //选中第一个
    var panels = $('#nav').accordion('panels');
    var t = panels[0].panel('options').title;
    $('#nav').accordion('select', t);
}
//获取左侧导航的图标
function getIcon(menuid) {
    var icon = 'icon ';
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                icon += o.icon;
            }
        })
    })

    return icon;
}

function addTab(title, href, icon) {
    var tt = $('#tabs');
    if (tt.tabs('exists', title)) {
        tt.tabs('select', title);
        refreshTab({ tabTitle: title, url: href });
    } else {
        var content = '未实现';
        if (href) {
            content = '<iframe scrolling="no" frameborder="0"  src="' + href + '" style="width:100%;height:100%;"></iframe>';
        }
        icon = "icon " + icon;
        tt.tabs('add', {
            title: title,
            closable: true,
            content: content,
            iconCls: icon
        });
    }
}
function closeTab() {
    var tab = $('#tabs').tabs('getSelected');
    $('#tabs').tabs("close", tab.panel('options').title);
}
function refreshTab(cfg) {
    var refresh_tab = cfg.tabTitle ? $('#tabs').tabs('getTab', cfg.tabTitle) : $('#tabs').tabs('getSelected');
    if (refresh_tab && refresh_tab.find('iframe').length > 0) {
        var _refresh_ifram = refresh_tab.find('iframe')[0];
        var refresh_url = cfg.url ? cfg.url : _refresh_ifram.src;
        //_refresh_ifram.src = refresh_url;   
        _refresh_ifram.contentWindow.location.href = refresh_url;
    }
}
